package com.huawei.myapplication;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.ScatterChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Banner banner;
    private TabLayout tab;
    private VideoView videoView;
    private ImageView imageView;
    private Button button;
    private Button button2;
    private Button button3;
    private BarChart barChart;
    private LineChart lineChart;
    private HorizontalBarChart horizontalBarChart;
    private PieChart pieChart;
    private ScatterChart scatterChart;
    private Banner banner2;
    private int isP = 0;
    Bitmap bitmap;
    String s;
    @SuppressLint("Range")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (isP == 0) {
            bitmap = (Bitmap) data.getExtras().get("data");
            Glide.with(imageView.getContext()).load(bitmap).into(imageView);
        } else {
            ContentResolver contentResolver = getContentResolver();
            Cursor cursor = contentResolver.query(data.getData(), new String[] {MediaStore.MediaColumns.DATA}, null, null, null);
            cursor.moveToNext();
            s = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
            Glide.with(imageView.getContext()).load(s).into(imageView);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE,Manifest.permission.CAMERA}, 1);
      }
        barChart.setNoDataText("loading...");
        lineChart.setNoDataText("loading...");
        horizontalBarChart.setNoDataText("loading...");
        scatterChart.setNoDataText("loading...");
        pieChart.setNoDataText("loading...");
        List<Integer> colors = new ArrayList<>(Arrays.asList(Color.GRAY, Color.YELLOW, Color.BLACK, Color.BLUE, Color.RED));
        banner.setAdapter(new BannerAd(Arrays.asList(R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4))).addBannerLifecycleObserver(this).start();
        List<BannerAd1.bannerInfo> bannerList = new ArrayList<>();
        bannerList.add(new BannerAd1.bannerInfo("2021-11-15", R.drawable.a1));
        bannerList.add(new BannerAd1.bannerInfo("2021-11-16", R.drawable.a2));
        bannerList.add(new BannerAd1.bannerInfo("2021-11-17", R.drawable.a3));
        banner2.setAdapter(new BannerAd1(bannerList)).addBannerLifecycleObserver(this).start();
        List<String> types = new ArrayList<>();
        types.add("1");
        types.add("2");
        types.add("3");
        tab.removeAllTabs();
        for (String s : types) {
            tab.addTab(tab.newTab().setText(s));
        }
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getText().toString()) {
                    case "1":
                        Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
                        break;
                    case "2":
                        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
                        break;
                    case "3":
                        Toast.makeText(MainActivity.this, "3", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.dyxxvideo01));
        MediaController mediaController = new MediaController(MainActivity.this);
        mediaController.setMediaPlayer(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isP = 0;
                startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 1);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isP = 1;
                startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), 1);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Intent.ACTION_DIAL).setData(Uri.parse("tel:" + "1008611")));
            }
        });
        List<BarEntry> list1 = new ArrayList<>();
        list1.add(new BarEntry(0, 5));
        list1.add(new BarEntry(2, 8));
        list1.add(new BarEntry(3, 2));
        list1.add(new BarEntry(4, 9));
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        x(barChart.getXAxis(), list);
        x(horizontalBarChart.getXAxis(), list);
        barChart.setData(new BarData(new BarDataSet(list1, "test1")));
        barChart.invalidate();
        horizontalBarChart.setData(new BarData(new BarDataSet(list1, "test3")));
        horizontalBarChart.invalidate();
        List<Entry> list2 = new ArrayList<>();
        list2.add(new Entry(0, 1));
        list2.add(new Entry(2, 8));
        list2.add(new Entry(3, 3));
        list2.add(new Entry(4, 6));
        x(lineChart.getXAxis(), list);
        x(scatterChart.getXAxis(), list);
        lineChart.setData(new LineData(new LineDataSet(list2, "test2")));
        lineChart.invalidate();
        scatterChart.setData(new ScatterData(new ScatterDataSet(list2, "test5")));
        scatterChart.invalidate();
        List<PieEntry> list3 = new ArrayList<>();
        list3.add(new PieEntry(20, "one"));
        list3.add(new PieEntry(30, "two"));
        list3.add(new PieEntry(80, "three"));
        list3.add(new PieEntry(100, "four"));
        PieDataSet pieDataSet = new PieDataSet(list3, "");
        pieDataSet.setColors(colors);
        PieData pieData = new PieData(pieDataSet);
        pieData.setValueFormatter(new PercentFormatter(pieChart));
        pieData.setValueTextColor(Color.WHITE);
        pieData.setValueTextSize(11f);
        pieChart.setHoleRadius(0);
        pieChart.setTransparentCircleRadius(0);
        pieChart.setUsePercentValues(true);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }

    public void x(XAxis xAxis, List<String> list) {
        xAxis.setValueFormatter(new IndexAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return list.get((int) value);
            }
        });
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setLabelCount(list.size(), false);
        xAxis.setLabelRotationAngle(45);
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        tab = (TabLayout) findViewById(R.id.tab);
        videoView = (VideoView) findViewById(R.id.videoView);
        imageView = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        barChart = (BarChart) findViewById(R.id.barChart);
        lineChart = (LineChart) findViewById(R.id.lineChart);
        horizontalBarChart = (HorizontalBarChart) findViewById(R.id.horizontalBarChart);
        pieChart = (PieChart) findViewById(R.id.pieChart);
        scatterChart = (ScatterChart) findViewById(R.id.scatterChart);
        banner2 = (Banner) findViewById(R.id.banner2);
    }
}